﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Structs;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CountryTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name", "format1", "format2", "format3_1|format3_2|format3_3|format3_4")]
        public void Can_CRUD(string code,
            string name, 
            string dateFormat, 
            string timeFormat,
            string rawNumericFormat)
        {
            var country = Processor.Execute(new CreateCountry
            {
                Code = code,
                Name = name,
                DateFormat = dateFormat,
                TimeFormat = timeFormat,
                NumericFormat = NumericFormat.Parse(rawNumericFormat)
            }).Result;

            Assert.NotNull(country);
            country.Code.AreEqualIgnoringCase(code);
            country.Name.AreEqualIgnoringCase(name);
            country.DateFormat.AreEqualIgnoringCase(dateFormat);
            country.TimeFormat.AreEqualIgnoringCase(timeFormat);
            country.NumericFormat.ToString().AreEqualIgnoringCase(rawNumericFormat);
            country.AreEqual(Processor.Execute(new GetCountry
            {
                EntityID = country.EntityID
            }).Result);
            country.AreEqual(Processor.Execute(new GetCountryByCode
            {
                Code = code
            }).Result);


            code = code.Reverse();
            name = name.Reverse();
            dateFormat = dateFormat.Reverse();
            timeFormat = timeFormat.Reverse();
            rawNumericFormat = rawNumericFormat.Reverse();
            var upd_country = Processor.Execute(new UpdateCountry
            {
                EntityID = country.EntityID,
                Code = code,
                Name = name,
                DateFormat = dateFormat,
                TimeFormat = timeFormat,
                NumericFormat = NumericFormat.Parse(rawNumericFormat),
                Flags = CountryFlags.Active
            }).Result;

            Assert.NotNull(upd_country);
            upd_country.AreEqual(country);
            upd_country.Code.AreEqualIgnoringCase(code);
            upd_country.Name.AreEqualIgnoringCase(name);
            upd_country.DateFormat.AreEqualIgnoringCase(dateFormat);
            upd_country.TimeFormat.AreEqualIgnoringCase(timeFormat);
            upd_country.NumericFormat.ToString().AreEqualIgnoringCase(rawNumericFormat);
            upd_country.Flags.AreEqual(CountryFlags.Active);

            Processor.Execute(new DeleteCountry
            {
                EntityID = country.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateCountry
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateCountry
            {
                Code = String.Empty
            }));
        }
    }
}
